<template>
  <div class="setting-r-content" style="overflow-y:auto;">
    <Row class="setting-r-c_params h1">
      <Col span="24" class="col_1">
        <Icon style="font-size:20px;" type="md-reorder" />
        <h2>报告生成</h2>
      </Col>
      <Col span="24" style="height:calc(100% - 24px);">
        <Card class="h1">
          <div style="width:40%;">
            <h3 style="font-size:20px;font-weight:bold;">生成上月报告：</h3>
            <div style="margin-top:20px;">
              <span>请选择企业：</span>
              <Select
                filterable
                clearable
                v-model="companyId"
                style="width:80%;"
              >
                <Option
                  v-for="item in companyList"
                  :value="item.id"
                  :key="item.value"
                  >{{ item.name }}</Option
                >
              </Select>
            </div>
            <div style="margin-top:20px;">
              <span>请选择报告类型：</span>
              <RadioGroup v-model="reportType">
                <Radio :label="1">用电分析报告</Radio>
                <Radio :label="2">能效报告</Radio>
              </RadioGroup>
            </div>
            <div style="margin-top:20px;text-align:center;">
              <Button type="primary" @click="handleLook">查看</Button>
              <Button style="margin-left:20px;" @click="handleDownload"
                >下载</Button
              >
            </div>
          </div>
        </Card>
      </Col>
    </Row>
  </div>
</template>
<script>
import { getCompanyListOfNew } from "@/api";
import { mapGetters } from "vuex";

const URL = process.env.VUE_APP_REPORT_URL;

export default {
  mounted() {
    this.getCompanyList();
  },
  computed: {
    ...mapGetters(["token"])
  },
  data() {
    return {
      companyId: "",
      reportType: 1,
      companyList: []
    };
  },
  methods: {
    getCompanyList() {
      const vm = this;
      getCompanyListOfNew({
        size: 100000
      }).then(res => {
        if (res.code !== 200) {
          vm.companyList = [];
        } else {
          vm.companyList = res.data.list;
        }
      });
    },

    handleLook() {
      if (!this.companyId) {
        this.$Message.warning("请选择一个企业");
        return;
      }
      const token = this.token;
      const companyId = this.companyId;
      if (this.reportType === 2) {
        // 能效报告
        const path = `${URL}/pc/effect/${companyId}/${token}/false`;
        window.open(path, "_blank");
      } else if (this.reportType === 1) {
        // 月度报告
        const path = `${URL}/pc/monthly/106/${companyId}/${token}/false`;
        window.open(path, "_blank");
      }
    },

    handleDownload() {
      if (!this.companyId) {
        this.$Message.warning("请选择一个企业");
        return;
      }
      const token = this.token;
      const companyId = this.companyId;
      if (this.reportType === 2) {
        const path = `${URL}/pc/effect/${companyId}/${token}/true`;
        window.open(path, "_blank");
      } else {
        const path = `${URL}/pc/monthly/106/${companyId}/${token}/true`;
        window.open(path, "_blank");
      }
    }
  }
};
</script>
